package net.northcoding.fresh.ast;

import net.northcoding.fresh.util.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

// @Author Diamond - Administrator
// @Date 2023/12/8 10:24:00

/*** Every program's root node
 *
 */
public class ProgramNode extends AstNode {

    private final Map<String, Object> identities = new HashMap<>();

    public ProgramNode() {
        identities.put("version", "1.0.0 01");
    }

    @Override
    public Object exec() {
        Vector<Object> results = new Vector<>();
        for (AstNode node : children) {
            results.add(node.exec());
        }
        return results;
    }

    @Override
    public Object getVar(java.lang.String s) throws IOException {
        if (!identities.containsKey(s)) {
            new Logger("Parser").error(java.lang.String.format("Variable '%s' is not exist", s));
            return null;
        }
        return identities.get(s);
    }

    @Override
    public void setVar(String key, Object value) {
        if (!identities.containsKey(key)) {
            this.identities.put(key, value);
        }
        try {
            new Logger("Parser").error(String.format("Variable '%s' is already exist", key));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
